﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MusicRipperDataAccess;

namespace UWSP_Music_Ripper
{
    class FileHelper
    {
       string errMessage;

       public string ErrMessage
       {
          get { return errMessage; }
          set { errMessage = value; }
       }
		#region Constructors (1) 

        public FileHelper()
        {

        }

		#endregion Constructors 

		#region Methods (2) 

		// Public Methods (1) 
       public static bool DoesDirectoryExist(string inDirName)
       {
          return Directory.Exists(inDirName);
       }
        public bool MoveFiles(string inRippedDirName, string inDestinationDir, Album inAlbum)
        {
           bool success = false;
            List<string> RippedFiles = GetTracksRipped(inRippedDirName, inAlbum.Artist, inAlbum.Title);
            if (RippedFiles.Count > 0)
            {
               string directoryToDelete = Path.Combine (inRippedDirName, inAlbum.Artist);
               //if (RippedFiles.Count != inAlbum.MyTracks.Count ())
               //{
               //   // clean out the dir no matter what
               //   Directory.Delete (directoryToDelete, true);
               //   throw new Exception ("mis matched number of files");
               //}
               for (int index = 0; index < RippedFiles.Count; index++)
               {
                  string file = Path.Combine (inDestinationDir, inAlbum.MyTracks[index].NewTitle);
                  if (File.Exists (file)) File.Delete (file);
                  File.Move (RippedFiles[index], file);
               }


               Directory.Delete (directoryToDelete, true);
               success = true;

            }
            else
            {
               this.errMessage = "No files were found to be ripped.  Check the format on Windows Media Player is set to WAV.";
            }
           
            return success;

        }
		// Private Methods (1) 

        private List<string> GetRippedTracksFileNames(List<string> inRippedTracks)
        {
            List<string> temp = new List<string>();
            foreach (string s in inRippedTracks)
            {
                temp.Add(Path.GetFileNameWithoutExtension(s));
            }
            return temp;
        }

        

		#endregion Methods 

        public List<string>GetTracksRipped(string inDirName, string inArtistName, string inAlbumName)
        {
           List<string> files= null;
            string directoryToSearch = Path.Combine(inDirName, inArtistName, inAlbumName);
            if (Directory.Exists (directoryToSearch))
            {
               files = Directory.GetFiles (directoryToSearch, "*.wav").ToList ();               
            }
            else
            {
               this.errMessage = directoryToSearch + " can not be found, please reset the out directory";
            }

            return files;
        }


    }
}
